﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessObject;
using YamahaTuanTruc.BusinessService;
using Qios.DevSuite.Components;

namespace YamahaTuanTruc.Controls
{
    public partial class QpBike : Qios.DevSuite.Components.QCompositeControl
    {
        public bool IsCreateNew { get; set; }
        public Xe bike { get; set; }
        public void OnLoad()
        {
            using (var service = new BusinessServiceClient())
            {
                KhachHang[] lstCus = service.GetListCustomer();
                if(lstCus != null && lstCus.Length > 0)
                {
                    foreach(var tmp in lstCus) 
                    {
                        qcibKhachHang.InputBox.Items.Add(tmp.Ten);
                    }
                }

                NhanVien[] lstStaff = service.getListStaff();
                if(lstStaff != null && lstStaff.Length > 0)
                {
                    foreach(var tmp in lstStaff)
                    {
                        qcibNhanVien.InputBox.Items.Add(tmp.Ten);
                    }
                }
            }
        }
        public QpBike()
        {
            InitializeComponent();
            //OnLoad();
        }
        public void InitializeState()
        {
            if (IsCreateNew)
            {
                qcibID.InputBox.Clear();
                qcibTen.InputBox.Clear();
                qcibMauSac.InputBox.Clear();
                //qcibNgayNhan.InputBox.Clear();
                IsCreateNew = false;
            }
            else
            {
                bike = new Xe();
                bike.ID = qcibID.InputBox.Text;
                bike.Ten = qcibTen.InputBox.Text;
                bike.MauSac = qcibMauSac.InputBox.Text;
                //bike.NgayNhanXe = Convert.ToDateTime(qcibNgayNhan.InputBox.Text);
                //bike.NhanVien.Ten = qcibNhanVien.InputBox.SelectedItem.ToString();
                bike.NhanVien.Ten = qcibNhanVien.InputBox.Text;
                //bike.KhachHang.Ten = qcibKhachHang.InputBox.SelectedItem.ToString();
                bike.KhachHang.Ten = qcibKhachHang.InputBox.Text;
            }

            qcbCapNhat.Enabled = !IsCreateNew;
            qcbTaoMoi.Enabled = !IsCreateNew;
            qcbThem.Enabled = IsCreateNew;
            qcbXoa.Enabled = !IsCreateNew;
        }

        private void qcibNhanVien_InputBox_SelectedItemChanged(object sender, EventArgs e)
        {
            MessageBox.Show(qcibNhanVien.InputBox.SelectedItem.ToString());
        }

        private void qcbTaoMoi_ItemActivated(object sender, QCompositeEventArgs e)
        {
            bike = null;
            IsCreateNew = true;
            InitializeState();
        }

        private void qcbThem_ItemActivated(object sender, QCompositeEventArgs e)
        {
            using (var service = new BusinessServiceClient())
            {
                InitializeState();
                bool res = service.AddBike(bike);
                if(res)
                {
                    MessageBox.Show("Thêm Xe thành công");
                }
                else
                {
                     MessageBox.Show("Thêm Xe thất bại, Xin vui lòng kiểm tra lại thông tin.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void qcbXoa_ItemActivated(object sender, QCompositeEventArgs e)
        {
            using(var service = new BusinessServiceClient())
            {
                bool res = service.DeleteBike(qcibID.InputBox.Text);
                if(res)
                {
                    MessageBox.Show("Xóa Xe thành công");
                }
                else
                {
                     MessageBox.Show("Xóa Xe thất bại, Xin vui lòng kiểm tra lại thông tin.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void qcbCapNhat_ItemActivated(object sender, QCompositeEventArgs e)
        {
            InitializeState();
            using(var service = new BusinessServiceClient())
            {
                bool res = service.UpdateBike(bike);
                if(res)
                {
                    MessageBox.Show("Cập nhật thông tin Xe thành công");
                }
                else
                {
                     MessageBox.Show("Cập nhật thông tin xe thất bại, Xin vui lòng kiểm tra lại thông tin.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void kryptonDateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }


    }
}